import { _decorator, Component, Sprite, SpriteFrame } from 'cc';
import { UIJoyStick } from './UIJoyStick';
const { ccclass, property } = _decorator;

@ccclass('PlaneTiltBase')
export class PlaneTiltBase extends Component {

    private lastAngle: number = 0;     // 上一次记录的角度
    public static lastChangeTime: number = -1;     // 上一次记录的角度
    private lastMinus: number = 0;     // 上一次记录的角度

    @property(SpriteFrame)
    private playerRightFrames:SpriteFrame[] = [];

    // 角度变化阈值，避免在边界频繁切换
    @property({ min: 1, max: 10, tooltip: '角度变化超过此值才更新图片' })
    private angleThreshold: number = 1.5;

    private gapTimeThreshold: number = 1200;

    private playerSpriteObj:Sprite;

    start() {

        this.playerSpriteObj = this.node.getComponent(Sprite);
    }

    update(deltaTime: number) {
        if (!UIJoyStick.isPlayerStop) {
            const angle = this.node.angle;
            if (angle > 0) {
                const currentAngle = this.node.angle;
                const minus = currentAngle - this.lastAngle;
                if (Math.abs(minus) >= this.angleThreshold) {
                    this.updatePlaneTiltv2();
                    this.lastAngle = currentAngle;
                    this.lastMinus = minus;
                } else {
                    // 如果没有变化，则复原
                    if (UIJoyStick.isTouching == true && PlaneTiltBase.lastChangeTime > 0 
                        && new Date().getTime() - PlaneTiltBase.lastChangeTime > this.gapTimeThreshold) {

                        this.fuyuan();

                        this.node.setScale(1,1,0);
                        this.lastMinus = 0;
                        PlaneTiltBase.lastChangeTime = -1;
                        
                    }
                }
            }
        }
    }

    private fuyuan() {
        const currentName = this.playerSpriteObj.spriteFrame.name;

        console.log("player复原。。。currentName:" + currentName)
        const num = parseInt(currentName);
        var idx = num;
        this.schedule(() => {
            this.playerSpriteObj.spriteFrame = this.playerRightFrames[idx--];

        }, 0.1, num, 0);
    }

    private updatePlaneTiltv2() {
        const spriteIdx = parseInt(this.playerSpriteObj.spriteFrame.name);

        const angle = this.node.angle;
        const changeJiaodu = Math.abs(angle - this.lastAngle);

        //console.log("lastAngle=" + this.lastAngle + " angle=" + angle + " changeJiaodu="  + changeJiaodu)

        if (this.lastMinus == 0) {
            return;
        }
    
        if (!this.jiaoduCheck(spriteIdx, changeJiaodu)) {
            PlaneTiltBase.lastChangeTime = new Date().getTime();
            //console.log("不变更帧。。。spriteIdx=" + spriteIdx + " changeJiaodu=" + changeJiaodu);
            return;
        }
    
        if (this.lastMinus > 0) {
            // 上次角度增大
            if (angle > this.lastAngle) {
                // 本次还是增大：持续增大，逆时针
                const idx = spriteIdx + 1;
                console.log("持续增大idx=" + idx)
                if (idx >= 0 && idx < this.playerRightFrames.length) {
                    this.playerSpriteObj.spriteFrame = this.playerRightFrames[idx];
                    this.node.setScale(-1, 1, 0);
                }
            } else {
                // 上次角度增大，本次减小：首次减小

                this.playerSpriteObj.spriteFrame = this.playerRightFrames[0];
                this.node.setScale(-1, 1, 0);
            }
        } else {
            // 上次角度减小
            if (angle < this.lastAngle) {
                // 本次减小：持续减小，顺时针
                const idx = spriteIdx + 1;
                if (idx >=0 && idx < this.playerRightFrames.length) {
                    this.playerSpriteObj.spriteFrame = this.playerRightFrames[idx];
                    this.node.setScale(1, 1, 0);
                }
            } else {
                // 本次增大
                this.playerSpriteObj.spriteFrame = this.playerRightFrames[0];
                this.node.setScale(-1, 1, 0);
            }
        }
        PlaneTiltBase.lastChangeTime = new Date().getTime();
    }

    private jiaoduCheck(spriteIdx:number, jiaodu:number):boolean {
        if (jiaodu < 0.5) {
            return false;
        }

        if (spriteIdx == 0) {
            return true;
        } else if (spriteIdx < 3) {
            if (jiaodu < 2) {
                return false;
            }
        } else if (spriteIdx == 3) {
            if (jiaodu < 6) {
                return false;
            }
        } else if (spriteIdx == 4) {
            if (jiaodu < 10) {
                return false;
            }
        } else if (spriteIdx == 5) {
            if (jiaodu < 30) {
                return false;
            }
        }
        return true;
    }

    public setPlayerAngle(angle:number) {
        this.node.angle = angle;
    }

    public onPlayerTouchEnd() {
        console.log("onPlayerTouchEnd...");
        this.fuyuan();
    }

    public getPlayerPosAndAngle():any {
        return {position:this.node.getPosition(), angle:this.node.angle}
    }
}

